find_package(Fcitx5Core REQUIRED)
find_package(Fcitx5Utils REQUIRED)
find_package(fmt REQUIRED)
find_package(Gettext REQUIRED)

set(E04INPUT_LIB_SOURCES
 KeyHandler.cpp
 LanguageModelLoader.cpp
 UTF8Helper.cpp
 Log.cpp  # Log is part of E04InputLib to facilitate debugging via logging.
 Engine/KeyValueBlobReader.cpp 
 Engine/E04InputLM.cpp
 Engine/ParselessLM.cpp
 Engine/ParselessPhraseDB.cpp
 Engine/PhraseReplacementMap.cpp
 Engine/UserOverrideModel.cpp
 Engine/UserPhrasesLM.cpp
 Engine/Mandarin/Mandarin.cpp)

# https://stackoverflow.com/questions/26549137/shared-library-on-linux-and-fpic-error
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
set(CXX_STANDARD_REQUIRED 20)

# Setup some compiler option that is generally useful and compatible with Fcitx 5 (C++17)
include("${FCITX_INSTALL_CMAKECONFIG_DIR}/Fcitx5Utils/Fcitx5CompilerSettings.cmake")

add_library(E04InputLib STATIC ${E04INPUT_LIB_SOURCES})
target_compile_options(E04InputLib PRIVATE -Wno-unknown-pragmas)
target_link_libraries(E04InputLib PRIVATE Fcitx5::Utils)
target_include_directories(E04InputLib PRIVATE Fcitx5::Utils)
target_compile_definitions(E04InputLib PRIVATE FCITX_GETTEXT_DOMAIN=\"fcitx5-e04input\")

include_directories(Engine)
include_directories(Engine/Gramambular)
include_directories(Engine/Mandarin)

add_library(e04input MODULE E04Input.cpp)
# Use this to build E04Input on, for example, Ubuntu 20.04 LTS
if (USE_LEGACY_FCITX5_API)
    MESSAGE(STATUS "Using legacy (pre-2021) Fcitx5 API")
    add_compile_definitions(e04input USE_LEGACY_FCITX5_API=1)
endif()
target_compile_options(e04input PRIVATE -Wno-unknown-pragmas)
target_link_libraries(e04input PRIVATE Fcitx5::Core E04InputLib fmt::fmt)
target_include_directories(e04input PRIVATE Fcitx5::Core fmt::fmt)
set_target_properties(e04input PROPERTIES PREFIX "")
target_compile_definitions(e04input PRIVATE FCITX_GETTEXT_DOMAIN=\"fcitx5-e04input\")
install(TARGETS e04input DESTINATION "${FCITX_INSTALL_LIBDIR}/fcitx5")

# Addon config file
# We need additional layer of conversion because we want PROJECT_VERSION in it.
configure_file(e04input-addon.conf.in.in e04input-addon.conf.in)
fcitx5_translate_desktop_file("${CMAKE_CURRENT_BINARY_DIR}/e04input-addon.conf.in" e04input-addon.conf)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/e04input-addon.conf" RENAME e04input.conf DESTINATION "${FCITX_INSTALL_PKGDATADIR}/addon")

# Input Method registration file
configure_file(e04input.conf.in.in e04input.conf.in)
fcitx5_translate_desktop_file("${CMAKE_CURRENT_BINARY_DIR}/e04input.conf.in" e04input.conf)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/e04input.conf" DESTINATION "${FCITX_INSTALL_PKGDATADIR}/inputmethod")

if (ENABLE_TEST)
    # Let CMake fetch Google Test for us.
    # https://github.com/google/googletest/tree/main/googletest#incorporating-into-an-existing-cmake-project
    include(FetchContent)

    FetchContent_Declare(
            googletest
            # Specify the commit you depend on and update it regularly.
            URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
    )
    # For Windows: Prevent overriding the parent project's compiler/linker settings
    set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
    FetchContent_MakeAvailable(googletest)

    include(GoogleTest)

    # Test target declarations.
    add_executable(E04InputTest
            KeyHandlerTest.cpp)
    target_compile_options(E04InputTest PRIVATE -Wno-unknown-pragmas)
    target_link_libraries(E04InputTest PRIVATE Fcitx5::Core gtest_main gmock_main E04InputLib)
    target_include_directories(E04InputTest PRIVATE Fcitx5::Core)

    configure_file(../data/data.txt e04input-test-data.txt)

    gtest_discover_tests(E04InputTest)

    add_custom_target(
            runTest
            COMMAND ${CMAKE_CURRENT_BINARY_DIR}/E04InputTest
    )
    add_dependencies(runTest E04InputTest)
endif ()
